package com.easyjava.qingke.project.command.impl;

import com.easyjava.qingke.project.command.SystemCommand;
import com.easyjava.qingke.project.mybatis.pojo.CollarSingle;
import com.easyjava.qingke.project.mybatis.pojo.CollarSingleML;
import com.easyjava.qingke.project.mybatis.pojo.DocumentRecord;
import com.easyjava.qingke.project.mybatis.pojo.Material;
import com.easyjava.qingke.project.mybatis.pojo.Project;
import com.easyjava.qingke.project.mybatis.pojo.PurchasePlanML;
import com.easyjava.qingke.project.mybatis.pojo.PurchasePlanning;
import com.easyjava.qingke.project.mybatis.pojo.User;
import com.easyjava.qingke.project.util.ProjectConsole;

public class AddCommand extends SystemCommand {

	public void excute() {

		ProjectConsole.println("1.添加用户");
		ProjectConsole.println("2.添加项目");
		ProjectConsole.println("3.添加材料");
		ProjectConsole.println("4.添加采购计划");
		ProjectConsole.println("5.添加领用单");
		ProjectConsole.println("6.添加文档");
		ProjectConsole.println("=================");

		int id = ProjectConsole.promptInt("请输入想要执行的操作的数字:");
		while (id > 0) {
			if (1==id) {
				addUser();
				break;
			} else if (2==id) {
				addProject();
				break;
			} else if (3==id) {
				addMaterial();
				break;
			} else if (4==id) {
				addPurchasePlan();
				break;
			} else if (5==id) {
				addCollarSingle();
				break;
			} else if (6==id) {
				addDocumentRecord();
				break;
			} else
				ProjectConsole.println("输入的数字不存在！");
			id = ProjectConsole.promptInt("请重新输入想要执行的操作的数字:");
		}
	}

	private void addDocumentRecord() {
		DocumentRecord documentRecord = new DocumentRecord();
		String code = ProjectConsole.prompt("请输入编码：");
		while (dao.isDocumentRecordCodeExists(code)) {
			ProjectConsole.println("该编码已被使用！");
			code = ProjectConsole.prompt("请重新输入编码：");
		}
		String description = ProjectConsole.prompt("请描述：");
		String type = ProjectConsole.prompt("请输入类型：");
		String concreteContent = ProjectConsole.prompt("请输入具体内容：");
		documentRecord.setCode(code);
		documentRecord.setDescription(description);
		documentRecord.setType(type);
		documentRecord.setConcreteContent(concreteContent);

		dao.addDocumentRecord(documentRecord);

	}

	private void addCollarSingle() {
		CollarSingle collarSingle = new CollarSingle();
		String code = ProjectConsole.prompt("请输入编码：");
		while (dao.isCollarSingleCodeExists(code)) {
			ProjectConsole.println("该编码已被使用！");
			code = ProjectConsole.prompt("请重新输入编码：");
		}
		String description = ProjectConsole.prompt("请描述：");
		dao.selectProject();
		ProjectConsole.println();
		int id = ProjectConsole.promptInt("请输入项目ID：");
		while (!dao.isProjectIdExists(id)) {
			ProjectConsole.println("该ID不存在！");
			id = ProjectConsole.promptInt("请重新输入项目ID：");
		}
		String name = ProjectConsole.prompt("请输入修改人姓名：");
		collarSingle.setCode(code);
		collarSingle.setDescription(description);
		collarSingle.setProjectId(id);
		collarSingle.setStatus("未出库");
		collarSingle.setLastModifiedPerson(name);

		dao.addCollarSingle(collarSingle);

		ProjectConsole.println("添加了领用单，需创建领用单材料清单！");
		dao.selectMaterial();
		ProjectConsole.println();

		CollarSingleML collarSingleML = new CollarSingleML();
		int materialId = ProjectConsole.promptInt("请输入材料ID：");
		while (!dao.isStorageMLMaterialExists(materialId)) {
			ProjectConsole.println("该材料ID不存在库中！");
			materialId = ProjectConsole.promptInt("请重新输入材料ID：");
		}
		int amount = ProjectConsole.promptInt("请输入数量：");
		while (amount > dao.getByMaterialId(materialId).getAmount()) {
			ProjectConsole.println("该数量超过了库存！");
			amount = ProjectConsole.promptInt("请重新输入数量：");
		}
		collarSingleML.setCollarSingleId(dao.getCollarSingleIdByCode(code));
		collarSingleML.setMaterialId(materialId);
		collarSingleML.setAmount(amount);

		dao.produceCollarSingleML(collarSingleML);

	}

	private void addPurchasePlan() {
		PurchasePlanning purchasePlan = new PurchasePlanning();
		String code = ProjectConsole.prompt("请输入编码：");
		while (dao.isPurchasePlanCodeExists(code)) {
			ProjectConsole.println("该编码已被使用！");
			code = ProjectConsole.prompt("请重新输入编码：");
		}
		String description = ProjectConsole.prompt("请描述：");
		dao.selectProject();
		ProjectConsole.println();
		int id = ProjectConsole.promptInt("请输入项目ID：");
		while (!dao.isProjectIdExists(id)) {
			ProjectConsole.println("该ID不存在！");
			id = ProjectConsole.promptInt("请重新输入项目ID：");
		}
		String name = ProjectConsole.prompt("请输入修改人姓名：");

		purchasePlan.setCode(code);
		purchasePlan.setDescription(description);
		purchasePlan.setProjectId(id);
		purchasePlan.setStatus("未审批");
		purchasePlan.setLastModifiedPerson(name);

		dao.addPurchasePlanning(purchasePlan);

		ProjectConsole.println("已成功创建采购计划，需生成采购计划材料清单！");
		dao.selectMaterial();
		ProjectConsole.println();
		PurchasePlanML purchasePlanML = new PurchasePlanML();
		int materialId = ProjectConsole.promptInt("请输入材料ID：");
		while (!dao.isMaterialIdExists(materialId)) {
			ProjectConsole.println("该ID不存在！");
			materialId = ProjectConsole.promptInt("请重新输入材料ID：");
		}
		int amount = ProjectConsole.promptInt("请输入数量：");
		purchasePlanML.setPurchasePlanningId(dao.getPurchaePlanningId(code));
		purchasePlanML.setMaterialId(materialId);
		purchasePlanML.setAmount(amount);

		dao.producePurchasePlanML(purchasePlanML);

	}

	private void addMaterial() {
		Material material = new Material();
		String code = ProjectConsole.prompt("请输入编码：");
		while (dao.isMaterialCodeExists(code)) {
			ProjectConsole.println("该编码已被使用！");
			code = ProjectConsole.prompt("请重新输入编码：");
		}
		String name = ProjectConsole.prompt("请输入名称：");
		while (dao.isMaterialNameExists(name)) {
			ProjectConsole.println("该名称已存在！");
			name = ProjectConsole.prompt("请重新输入名称：");
		}
		String type = ProjectConsole.prompt("请输入型号：");
		String unit = ProjectConsole.prompt("请输入单位：");
		material.setCode(code);
		material.setName(name);
		material.setType(type);
		material.setUnit(unit);

		dao.addMaterial(material);
	}

	private void addProject() {
		Project project = new Project();
		String code = ProjectConsole.prompt("请输入编码：");
		while (dao.isProjectCodeExists(code)) {
			ProjectConsole.println("该编码已被使用！");
			code = ProjectConsole.prompt("请重新输入编码：");
		}
		String name = ProjectConsole.prompt("请输入名称：");
		while (dao.isProjectNameExists(name)) {
			ProjectConsole.println("该名称已存在！");
			name = ProjectConsole.prompt("请重新输入名称：");
		}
		String buildUnit = ProjectConsole.prompt("请输入建设单位：");
		String firstParty = ProjectConsole.prompt("请输入甲方：");
		project.setCode(code);
		project.setName(name);
		project.setBuildUnit(buildUnit);
		project.setFirstParty(firstParty);

		dao.addProject(project);

	}

	private void addUser() {
		User user = new User();
		String name = ProjectConsole.prompt("请输入用户名：");
		while (dao.isUserNameExists(name)) {
			ProjectConsole.println("该用户已存在！");
			name = ProjectConsole.prompt("请重新输入用户名：");
		}
		String phone = ProjectConsole.prompt("请输入手机号：");
		String email = ProjectConsole.prompt("请输入邮箱号：");

		user.setName(name);
		user.setPhone(phone);
		user.setEmail(email);

		dao.addUser(user);

	}

}
